package com.solidit.cassandra.controller.persistance.util;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore;
import org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.AsyncClient.get_all_tables_call;
import org.apache.hadoop.hive.service.HiveClient;
import org.apache.hadoop.hive.service.ThriftHive;
import org.apache.hadoop.hive.service.ThriftHive.AsyncClient.fetchAll_call;
import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;
import org.apache.thrift.transport.TSocket;

public class Hive {
	static class TestCallback implements
			AsyncMethodCallback<ThriftHiveMetastore.AsyncClient.get_all_tables_call> {

		@Override
		public void onComplete(get_all_tables_call arg0) {
			System.out.println("Termino!!!");

		}

		@Override
		public void onError(Exception arg0) {
			System.out.println("quedo la caga");
			arg0.printStackTrace();
		}

	}

	static ThriftHive.AsyncClient client;

	public static void main(String[] args) throws TException, IOException {

		TNonblockingTransport transport = new TNonblockingSocket("127.0.0.1",
				10000);
		TAsyncClientManager clientManager = new TAsyncClientManager();
		TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
		// transport.open();
		client = new ThriftHive.AsyncClient(protocolFactory, clientManager,
				transport);
		//query("use prueba", new TestCallback());
		//query("show tables", new TestCallback());
		client.get_all_tables("prueba",  new TestCallback());
		client.fetchAll(new AsyncMethodCallback<ThriftHive.AsyncClient.fetchAll_call>() {

			@Override
			public void onComplete(fetchAll_call arg0) {
				System.out.println("y ñacpm");
				
			}

			@Override
			public void onError(Exception arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		final TSocket tSocket = new TSocket("127.0.0.1", 10000);
		final TProtocol protocol = new TBinaryProtocol(tSocket);
		final HiveClient client = new HiveClient(protocol);
		tSocket.open();
		client.execute("use prueba");
		client.execute("show tables");
		final List<String> results = client.fetchAll();
		for (String result : results) {
			System.out.println(result);
		}
		tSocket.close();
		try {
			Thread.sleep(10000 * 60);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void query(
			String query,
			org.apache.thrift.async.AsyncMethodCallback<ThriftHive.AsyncClient.execute_call> resultHandler)
			throws org.apache.thrift.TException {
		client.execute(query, resultHandler);

	}

}
